<template>
    <div class="content">
        <div class="logo-view">
            <img src="@/assets/images/logo.svg">
            <span>{{ title }}</span>
        </div>
        <div class="bg-banner"/>
        <div id="login-box">
            <el-form ref="form" :model="form" :rules="rules" class="login-form" auto-complete="on"
                     label-position="left">
                <div class="title-container">
                    <h3 class="title">系统登陆</h3>
                </div>
                <div>
                    <el-form-item prop="account">
                        <el-input ref="name" v-model="form.username" placeholder="用户名" type="text" tabindex="1"
                                  auto-complete="on">
                            <svg-icon slot="prefix" icon-class="user"/>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input ref="password" v-model="form.password" :type="passwordType" placeholder="密码"
                                  tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin">
                            <svg-icon slot="prefix" icon-class="password"/>
                            <svg-icon slot="suffix" :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
                                      @click="showPassword"/>
                        </el-input>
                    </el-form-item>
                    <el-form-item prop="code" v-if="captchaEnabled">
                        <el-input
                            v-model="form.code"
                            auto-complete="off"
                            placeholder="验证码"
                            style="width: 63%"
                            @keyup.enter.native="handleLogin"
                        >
                            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
                        </el-input>
                        <div class="login-code">
                            <img :src="codeUrl" @click="getCode" class="login-code-img"/>
                        </div>
                    </el-form-item>
                </div>
                <el-checkbox v-model="form.remember">记住我</el-checkbox>
                <el-button :loading="loading" type="primary" style="width: 100%;" @click.native.prevent="handleLogin">登
                    录
                </el-button>
                <div
                    style="display:none;margin-top: 20px; margin-bottom: -10px; color: #666; font-size: 14px; text-align: center; font-weight: bold;">
                    <span style="margin-right: 5px;">演示帐号一键登录：</span>
                    <el-button type="danger" size="mini" @click="testAccount('admin')">admin</el-button>
                    <el-button type="danger" size="mini" plain @click="testAccount('test')">test</el-button>
                </div>
            </el-form>
            <Copyright v-if="$store.state.settings.showCopyright"/>
        </div>
    </div>
</template>

<script>
import Cookies from "js-cookie";
import {getCodeImg} from "@/api/system/login";
import {decrypt, encrypt} from "@/util/jsencrypt";

export default {
    name: 'Login',
    data() {
        return {
            title: process.env.VUE_APP_TITLE,
            form: {
                username: localStorage.login_account || '',
                password: '',
                code: '',
                remember: !!localStorage.login_account
            },
            rules: {
                username: [
                    {required: true, trigger: 'blur', message: '请输入用户名'}
                ],
                password: [
                    {required: true, trigger: 'blur', message: '请输入密码'},
                    {min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位'}
                ]
            },
            loading: false,
            passwordType: 'password',
            redirect: undefined,
            codeUrl: '',
            captchaEnabled: true,
        }
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    created() {
        this.getCode();
        this.getCookie();
    },
    methods: {
        /**
         * 获取Cookie
         * */
        getCookie() {
            const username = Cookies.get("username");
            const password = Cookies.get("password");
            const rememberMe = Cookies.get('rememberMe')
            this.form = {
                username: username === undefined ? this.form.username : username,
                password: password === undefined ? this.form.password : decrypt(password),
                rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
            };
        },
        /**
         * 显示密码
         * */
        showPassword() {
            this.passwordType = this.passwordType === 'password' ? '' : 'password'
            this.$nextTick(() => {
                this.$refs.password.focus()
            })
        },
        /**
         * 登陆
         * */
        handleLogin() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    // this.loading = true
                    // this.$store.dispatch('user/userLogin', this.form).then(() => {
                    //     this.loading = false
                    //     this.form.remember && localStorage.setItem('login_account', this.form.account)
                    //     this.$router.push({path: this.redirect || '/'})
                    // }).catch(() => {
                    //     this.loading = false
                    // })
                    this.loading = true;
                    if (this.form.remember) {
                        Cookies.set("username", this.form.username, {expires: 30});
                        Cookies.set("password", encrypt(this.form.password), {expires: 30});
                        Cookies.set('rememberMe', this.form.remember, {expires: 30});
                    } else {
                        Cookies.remove("username");
                        Cookies.remove("password");
                        Cookies.remove('rememberMe');
                    }
                    this.$store.dispatch("user/Login", this.form).then((res) => {
                        this.loading = false
                        this.form.remember && localStorage.setItem('login_account', this.form.account)
                        this.$router.push({path: this.redirect || '/'})
                    }).catch(() => {
                        this.loading = false;
                        if (this.captchaEnabled) {
                            this.getCode();
                        }
                    });
                }
            })
        },
        /**
         * 获取验证码图片
         * */
        getCode() {
            getCodeImg().then(res => {
                this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
                if (this.captchaEnabled) {
                    this.codeUrl = "data:image/gif;base64," + res.img;
                    this.form.uuid = res.uuid;
                }
            });
        },
        testAccount(account) {
            this.form.account = account
            this.form.password = '123456'
            this.handleLogin()
        }
    }
}
</script>

<style lang="scss" scoped>
[data-mode=mobile] {
    #login-box {
        max-width: 80%;
    }
}

.content {
    min-width: 1160px;
}

::v-deep input[type=password]::-ms-reveal {
    display: none;
}

.logo-view {
    position: absolute;
    z-index: 10;
    bottom: 10px;
    left: 10px;
    display: flex;
    justify-content: center;
    align-items: center;

    span {
        font-size: 20px;
        font-weight: 800;
        margin-left: 10px;
        color: #666;
    }

    img {
        width: 40px;
    }
}

.bg-banner {
    position: absolute;
    z-index: 0;
    width: 100%;
    min-width: 1160px;
    height: 100%;
    background-image: url(../../../assets/images/login-bg.jpg);
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center center;
}

#login-box {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 50%;
    height: 100%;
    width: 36vw;
    min-width: 413px;
    left: 82%;
    transform: translateX(-50%) translateY(-50%);
    background: rgba(255, 255, 255, 0.8);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 0 5px #999;

    .login-form {
        width: 450px;
        padding: 50px 35px 30px;
        overflow: hidden;

        .svg-icon {
            vertical-align: -0.35em;
        }

        .title-container {
            position: relative;

            .title {
                font-size: 22px;
                color: #666;
                margin: 0 auto 30px;
                text-align: left;
                font-weight: bold;
                @include text-overflow;
            }
        }
    }

    ::v-deep .el-input {
        display: inline-block;
        height: 48px;
        width: 100%;

        input {
            height: 48px;
        }

        .el-input__prefix {
            left: 10px;
        }

        .el-input__suffix {
            right: 10px;
        }
    }

    .el-checkbox {
        margin: 20px 0;
    }
}

.copyright {
    position: absolute;
    bottom: 30px;
    width: 100%;
    margin: 0;
    mix-blend-mode: difference;
}


.login-code {
    width: 35%;
    height: 47px;
    float: right;

    img {
        cursor: pointer;
        vertical-align: middle;
    }
}

.login-code-img {
    height: 47px;
}
</style>
